import cv2
import threading
import queue
import numpy as np
from UI import Mywindow
from tkinter import *
from PIL import Image,ImageTk
from predict_model import predict_rubbish,predict_run



new_width=500
new_height=500



def fenlei():
    print("垃圾分类成功！")


class CameraThread(threading.Thread):
    def __init__(self, camera_index, model, queue):
        threading.Thread.__init__(self)
        self.camera_index = camera_index
        self.model = model
        self.queue = queue
        # self.squeue=squeue
        self.state = "系统加载中……"
    def run(self):
        cap = cv2.VideoCapture(self.camera_index)
        count=0
        while True:
            ret, frame = cap.read()
            if not ret:
                break

            self.queue.put([frame,self.state])
            # print(frame)
            # print("即将开始预测……")

            count=count+1
            if count%200==0:
                frame = self.queue.get()[0]
                print("正在分类中……")
                print("开始预测！")
                image=frame
                self.state=predict_run(image)
                # self.squeue.put(result)
                # result = self.model(frame)
                # cv2.imshow("Predictions", result)
                cv2.waitKey(1)


class DisplayThread(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
        # self.squeue=squeue

    def run(self):
        while True:
            # 从队列中获取帧并实时显示
            if not self.queue.empty():
                frame = self.queue.get()[0]
                # cv2.imshow("Camera Feed", frame)
                if cv2.waitKey(1) & 0xFF == ord('q'):  # 等待按键
                    break


class DisplayUi(threading.Thread):
    def __init__(self,queue):
        threading.Thread.__init__(self)
        # self.run()
        self.queue = queue
        # self.squeue=squeue

    def run(self):
        root = Tk()
        # image_path = "C:/Users/86167/Desktop/program/garbage/garbage/1/img_331.jpg"
        # image = Image.open(image_path)

        # image=self.queue.get()
        # # image = cv2.imencode('.jpg', image)[1].tostring()
        # # cv2.imshow("image",image)
        #
        # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        # image = Image.fromarray(image)
        # image = image.resize((new_width, new_height))
        # image = ImageTk.PhotoImage(image)
        app = Mywindow(frame_queue,root)
        # app.update_image()
        root.mainloop()


if __name__ == "__main__":

    model = ...  # TODO: 替换为你的模型定义和参数初始化
    frame_queue = queue.Queue()
    state_queue = queue.Queue()
    camera_thread = CameraThread(0, model, frame_queue)
    display_thread = DisplayThread(frame_queue)
    display_ui= DisplayUi(frame_queue)
    camera_thread.start()
    display_thread.start()
    display_ui.start()
    camera_thread.join()
    display_thread.join()
    display_ui.join()

